package com.bhf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bhf.mapper.SysUserMapper;
import com.bhf.model.entity.CustomUser;
import com.bhf.model.entity.SysUser;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;

/**
 * @author pwq
 */
@Service
public class MyUserDetailsServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String name){
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUsername,name);
        SysUser sysUser = this.baseMapper.selectOne(wrapper);
        if (null == sysUser){
            throw new UsernameNotFoundException("用户名输入错误，请重新输入!");
        }
        List<SimpleGrantedAuthority> list = new LinkedList<>();
        list.add(new SimpleGrantedAuthority("ADMIN"));
        list.add(new SimpleGrantedAuthority("ROOT"));
        return new CustomUser(sysUser.getId(),sysUser.getUsername(),sysUser.getPassword(), list);
    }
}
